/*
 * @Description: 备份计划
 * @Author: Zzt
 * @Date: 2022-08-12 14:29:02
 * @LastEditTime: 2022-08-16 10:40:10
 */

import { createContext, useReducer, useEffect, useContext } from 'react';
import {
  SET_SELECT_AT,
  SET_SELECT_AT_ID,
  SET_SELECT_FOND,
  SET_SELECT_FOND_ID,
  SET_TRANSFER_LIST,
  DELETE_PLAN_ITEM,
  SET_SELECTED_ITEM
} from '../constants';

import { BackupPlanReducer } from '../reducers/BackupPlanReducer';
import { Button, Modal } from 'antd';
import { deleteBackup } from '@/api/archives/backupPlan';
import { fetchTransferListByPage } from '@/api/transfer';
import style from '../styles/context.module.scss';

const initialState = { transferList: [], delId: [] };
const BackupPlanContext = createContext();

const BackupPlanContextProvider = (props) => {
  const [state, dispatch] = useReducer(BackupPlanReducer, initialState);
  const actions = {
    /**
     * @description: 设置选中的全宗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    /**
     * @description: 设置选中的全宗
     */
    setSelectFondId(id) {
      dispatch({ type: SET_SELECT_FOND_ID, id });
    },

    /**
     * @description: 设置选中的档案类型ID
     */
    setSelectATId(id) {
      dispatch({ type: SET_SELECT_AT_ID, id });
    },
    /**
     * @description: 获取移交清单列表
     */
    getTransferList(params) {
      return new Promise((resolve, reject) => {
        fetchTransferListByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_TRANSFER_LIST, data: data.results });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /**
     * @description: 设置表格中选中条目的key
     * @param {*} selectArr
     * @return {*}
     */
    setSelectKey(selectArr) {
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },
    /**
     * @description: 删除计划
     * @param {number|number[]} id
     * @param {string} title
     */
    deleteBackupPlan(id) {
      console.log('Fun', id);
      Modal.confirm({
        title: '警示',
        okText: '确认删除',
        cancelText: '取消',
        content: (
          <div>
            <div>请谨慎操作：系统不建议删除计划</div>
            <div>删除所属计划，备份信息将丢失</div>
            <div className={style.but}>
              如希望取消此计划的执行，可使用
              <Button type="link" size="small">
                【暂停计划】
              </Button>
            </div>
          </div>
        ),
        // footer:{[<><Button>取消</Button><Button>确认删除</Button></>]},
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteBackup(ids).then((res) => {
            const data = res.data.map((item) => parseInt(item, 10));
            dispatch({ type: DELETE_PLAN_ITEM, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    },
    /* 手动备份 */
    manual(id) {
      console.log('Fun', id);
      Modal.confirm({
        title: '手动备份',
        okText: '确认',
        cancelText: '取消',
        content: (
          <div className={style.modal}>
            <div>您正在执行手动执行备份，备份过程详见【备份清单】</div>
          </div>
        ),
        // footer:{[<><Button>取消</Button><Button>确认删除</Button></>]},
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteBackup(ids).then((res) => {
            const data = res.data.map((item) => parseInt(item, 10));
            dispatch({ type: DELETE_PLAN_ITEM, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    }
  };
  useEffect(() => {}, []);
  return (
    <BackupPlanContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </BackupPlanContext.Provider>
  );
};
export const useBackupPlanContext = () => {
  return useContext(BackupPlanContext);
};
export { BackupPlanContext, BackupPlanContextProvider };
